﻿module Runner {
    export class BattleBackground extends Phaser.Sprite {
        //  From http://glslsandbox.com/e#16153.0
        webGLFilterText: string[] = [
            "precision mediump float;",

            "uniform float     time;",
            "uniform vec2      resolution;",
            "uniform vec2      mouse;",

            "#define MAX_ITER 4",

            "void main( void )",
            "{",
            "vec2 v_texCoord = gl_FragCoord.xy / resolution;",

            "vec2 p =  v_texCoord * 8.0 - vec2(20.0);",
            "vec2 i = p;",
            "float c = 1.0;",
            "float inten = .05;",

            "for (int n = 0; n < MAX_ITER; n++)",
            "{",
            "float t = time * (1.0 - (3.0 / float(n+1)));",

            "i = p + vec2(cos(t - i.x) + sin(t + i.y),",
            "sin(t - i.y) + cos(t + i.x));",

            "c += 1.0/length(vec2(p.x / (sin(i.x+t)/inten),",
            "p.y / (cos(i.y+t)/inten)));",
            "}",

            "c /= float(MAX_ITER);",
            "c = 1.5 - sqrt(c);",

            "vec4 texColor = vec4(0.039, 0.00, 0.072, 1.0);",

            "texColor.rgb *= (1.0 / (1.0 - (c + 0.05)));",

            "gl_FragColor = texColor;",
            "}"
        ];

        webGlFilter: Phaser.Filter;
        backgroundSprite: Phaser.Sprite;

        constructor(game:Phaser.Game) {
            super(game, 0, 0);

            this.webGlFilter = new Phaser.Filter(this.game, null, this.webGLFilterText);
            this.webGlFilter.setResolution(GAMEWIDTH, GAMEHEIGHT);
            this.backgroundSprite = this.game.add.sprite(0, 0);
            this.backgroundSprite.width = GAMEWIDTH;
            this.backgroundSprite.height = GAMEHEIGHT;

            this.backgroundSprite.filters = [this.webGlFilter];

        }

        destroy() {
            this.webGlFilter.destroy();
            this.backgroundSprite.destroy(true);
        }

        update() {
            this.webGlFilter.update(this.game.input.activePointer);
        }
    }
}